
using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Collections.Generic;

namespace bjeschool
{
	public partial class ClassNoteEdit : UIViewController
	{
		private UIPageViewController pagectrl;
		[Outlet("page")]
		private UIPageControl page { get; set; }

		public ClassNoteEdit () : base ("ClassNoteEdit", null)
		{
		}
		
		public override void DidReceiveMemoryWarning ()
		{
			// Releases the view if it doesn't have a superview.
			base.DidReceiveMemoryWarning ();
			
			// Release any cached data, images, etc that aren't in use.
		}

		private List<UIViewController> list = new List<UIViewController>();
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();

			// Perform any additional setup after loading the view, typically from a nib.
			var ctrls = new UIViewController[]{new ClassNoteEditStep1(), new ClassNoteEditStep2(), new ClassNoteEditStep3()};
			list.AddRange(ctrls);
			pagectrl = new UIPageViewController(UIPageViewControllerTransitionStyle.Scroll, 
			                                    UIPageViewControllerNavigationOrientation.Horizontal,
			                                    UIPageViewControllerSpineLocation.None, 20f);	
			pagectrl.SetViewControllers(new UIViewController[]{ctrls[0]}, UIPageViewControllerNavigationDirection.Forward, true, CompletePages);
			this.View.AddSubview(pagectrl.View);
			pagectrl.DataSource = new MyDataSource (list, page);
		}
		
		class MyDataSource : UIPageViewControllerDataSource {

			List<UIViewController> list;
			private UIPageControl page;
			public MyDataSource(List<UIViewController> list, UIPageControl page)
			{
				this.list = list;
				this.page = page;
			}

			public override UIViewController GetPreviousViewController (UIPageViewController pageViewController, 
			                                                            UIViewController referenceViewController)
			{
				int index = list.IndexOf(referenceViewController);
				if(index==0)
					return null;
				page.CurrentPage = index-1;
				return list[page.CurrentPage];
			}
			
			public override UIViewController GetNextViewController (UIPageViewController pageViewController, 
			                                                        UIViewController referenceViewController)
			{
				int index = list.IndexOf(referenceViewController);
				if(index==list.Count-1)
					return null;
				page.CurrentPage = index+1;
				return list[page.CurrentPage];
			}
		}

		private void CompletePages (bool complete)
		{
		}
		
		public override void ViewDidUnload ()
		{
			base.ViewDidUnload ();
			
			// Clear any references to subviews of the main view in order to
			// allow the Garbage Collector to collect them sooner.
			//
			// e.g. myOutlet.Dispose (); myOutlet = null;
			
			ReleaseDesignerOutlets ();
		}
		
		public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
		{
			// Return true for supported orientations
			return true;
		}
	}
}

